﻿package com.saint.utils.ui{
	
	import flash.display.Sprite;
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import com.saint.utils.events.ButtonEvent;
	import com.saint.utils.Logger;
	
	public class GraphicBttn extends Sprite {
		
		public static const BUTTON_PRESS:String = "button press";
		public static const BUTTON_RELEASE:String = "button release";
		
		private var defaultColour:uint = 0x999900;
		private var defaultHeight:uint = 50;
		private var defaultWidth:uint = 50;
		private var disabled:*;
		private var bg:*;
		private var ro:*;
		private var overlay_up:*;
		private var overlay_over:*;
		
		public function GraphicBttn(_bg:* = null, _ro:* = null, _off:* = null, _overlay_up:* = null, _overlay_over:* = null){
			
			if(_bg != null){
				bg = new _bg;
				addChild(bg);
			}else {
				drawBg();
			}
			
			if(_ro != null){
				ro = new _ro;
				ro.visible = false;
				addChild(ro);
			}
			
			if(_off != null){
				disabled = new _off;
				addChild(disabled);
			}
			
			if (_overlay_up != null) {
				overlay_up = new _overlay_up;
				addChild(overlay_up);
			}
			
			if (_overlay_over != null) {
				overlay_over = new _overlay_over;
				overlay_over.visible = false;
				addChild(overlay_over);
			}
			
			setEnabled(true);
		}
		
		private function handleMouseUp(e:MouseEvent):void{
			if(ro != null){
				ro.visible = false;
			}
			dispatchEvent(new ButtonEvent(ButtonEvent.BUTTON_RELEASE, this));
		}
		
		private function handleMouseDown(e:MouseEvent):void {
			stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp, false, 0, true);
			dispatchEvent(new ButtonEvent(ButtonEvent.BUTTON_PRESS, this));
		}
		
		private function handleMouseOver(e:MouseEvent):void {
			if(ro != null){
				ro.visible = true;
			}
			if (overlay_over != null) {
				overlay_over.visible = true;
			}
		}
		
		private function handleMouseOut(e:MouseEvent):void{
			if (ro != null) {
				ro.visible = false;
			}
			if (overlay_over != null) {
				overlay_over.visible = false;
			}
		}
		
		protected function onStageMouseUp(event:MouseEvent):void {
			Logger.info("GraphicBttn.onStageMouseUp ");
			if (event.target == this ){
				// already handled
			}else{
				handleMouseUp(null);
			}
			
			try {
				stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
			}catch(err:Error) {
				Logger.info("GraphicBttn - no stage event to remove");
			}
		}
		
		public function setEnabled(TF:Boolean):void{
			if(TF){
				buttonMode = true;
				mouseChildren = false;
				addEventListener(MouseEvent.ROLL_OVER, handleMouseOver, false, 0, true);
				addEventListener(MouseEvent.ROLL_OUT, handleMouseOut, false, 0, true);
				addEventListener(MouseEvent.MOUSE_UP, handleMouseUp, false, 0, true);
				addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown, false, 0, true);
			}else{
				buttonMode = false;
				removeEventListener(MouseEvent.ROLL_OVER, handleMouseOver);
				removeEventListener(MouseEvent.ROLL_OUT, handleMouseOut);
				removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
				removeEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
				stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
			}
			if (disabled != null && disabled.parent != null) {
				disabled.visible = !TF;
			}
		}
		
		private function drawBg():void {
			graphics.beginFill(defaultColour, 1);
			graphics.drawRect(0, 0, defaultWidth, defaultHeight);
			graphics.endFill();
		}
		
		public function destroy():void {
			Logger.info("GraphicBttn.destroy");
			if (hasEventListener(MouseEvent.ROLL_OVER)){
				removeEventListener(MouseEvent.ROLL_OVER, handleMouseOver);
				removeEventListener(MouseEvent.ROLL_OUT, handleMouseOut);
				removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
				removeEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
			}
		}
		
		
	}
}